package com.example.fengshoutong.utils;

import android.app.Activity;
import android.os.Handler;
import android.widget.Toast;

/**
 * Toast工具栏，用于弹出Toast提示用户
 */
public class ToastUtils {
    private static ToastUtils instance;
    private Activity activity;
    private Handler handler;

    /**
     * 单例模式
     *
     * @return
     */
    public static ToastUtils getInstance() {
        if (instance == null) {
            instance = new ToastUtils();
        }
        return instance;
    }

    public void init(Activity activity) {
        this.activity = activity;
        handler = new Handler(activity.getMainLooper());
    }

    /**
     * 在主线程中弹出Toast
     *
     * @param msg
     */
    public void makeToast(String msg) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
